package com.ioffice.export.excel.engine.data.excel.impl.tree;

import java.util.List;

import com.ioffice.exception.IOfficeException;
import com.ioffice.export.excel.data.ExcelSheetDataInfo;
import com.ioffice.export.excel.engine.data.excel.impl.AbstractEngineDataSheetCompent;
import com.ioffice.export.excel.sheet.cell.SheetCell;
import com.ioffice.export.excel.sheet.tree.SheetTopHeader;
import com.ioffice.export.excel.type.ExcelSheetTreeType;
import com.ioffice.export.excel.type.ExcelSheetType;
import static com.ioffice.export.excel.engine.data.excel.EngineDataSheetCell.getSheetCell;
import static com.ioffice.export.excel.engine.data.excel.EngineDataSheetCell.addChidrenSheetCell;




public class EngineDataSheetTreeTopHeader extends AbstractEngineDataSheetCompent{

	private int leafCount=0;
	private int deepCount=0;
	private int leftLeafChildCount=0;
	
	private ExcelSheetDataInfo parentData;
	private SheetCell cell;
	private ExcelSheetDataInfo childData;
	
	
	public EngineDataSheetTreeTopHeader() {
		compent=new SheetTopHeader();
	}
	
	
	
	@Override
	public void data2SheetCompent(List<ExcelSheetDataInfo> datas,ExcelSheetType sheetType) throws IOfficeException {
		ExcelSheetTreeType treeType=(ExcelSheetTreeType)sheetType;
		leafCount=0;
		for(int i=0,size=datas.size();i<size;i++){
			parentData=datas.get(i);
			if(parentData.getChildLevel()==0){//root
				if(parentData.getParentId()==treeType.getTopHeaderParentId()){
					cell=getSheetCell(parentData);
					boolean isHaveChild=true;
					for(int j=i+1;j<datas.size();j++){
						childData=datas.get(j);
						//是否为第一级节点并第一级节点的parentId为root节点的id
						if(childData.getChildLevel()==1&&childData.getParentId()==parentData.getId()){
							deepCount=1;
							cell=getAllChildSheetCell(cell, datas, childData.getParentId(), j, 1);
							isHaveChild=false;
						}
					}
					if(isHaveChild){//没有子节点
						cell=setOnlyParentCell(cell, 0);
					}
					((SheetTopHeader)compent).addChild(cell);
				}
			}
		}
		((SheetTopHeader)compent).setDeepCount(deepCount);
	}


	public int getSheetTopHeaderNodesLeaf(SheetTopHeader top) throws IOfficeException{
		int childCount =top.getChildSize();
		leftLeafChildCount=0;
		for(int i=0;i<childCount;i++){
			getSheetGenericNodesLeaf(top.getSheetCell(i));
		}
		return leftLeafChildCount;
	}
	

	private SheetCell getAllChildSheetCell(SheetCell parentCell,List<ExcelSheetDataInfo> datas, int parentId,int stemp,int topDeepCount) {
		ExcelSheetDataInfo info=datas.get(stemp);
		SheetCell cell=null;
		if(info.getParentId()==parentId){
			topDeepCount++;
			cell=getSheetCell(info);
			for(int i=stemp+1;i<datas.size();i++){
				cell=getAllChildSheetCell(cell, datas, info.getId(), i, topDeepCount);
			}
			if(topDeepCount>deepCount){
				deepCount=topDeepCount;
			}
			addChidrenSheetCell(parentCell, cell);
		}
		
		return parentCell;
	}
	
	private SheetCell setOnlyParentCell(SheetCell cell,int topDeepCount){
		topDeepCount++;
		if(topDeepCount>deepCount){
			deepCount=topDeepCount;
		}
		leafCount++;
		return cell;
	}

	private  void getSheetGenericNodesLeaf(SheetCell cell){
		int childCount =cell.getChildSize();
		if(childCount==0)
			leftLeafChildCount++;
		else{
			for(int i=0;i<childCount;i++){
				getSheetGenericNodesLeaf(cell.getSheetCell(i));
			}
		}
	}
}
